
The \emph{Jena framework} \cite{Jena}  was used to query DBpedia and retrieve semantic information.
Our queries followed the following format:
\begin{lstlisting}
SELECT * WHERE {
	<resource1> <attribute1> <resource2>
}
\end{lstlisting}

where ``attribute1" is the name of the relationship that ``resource1" shares with ``resource2". We are quering for resources in particular, and not literals - for example the \emph{abstract} of a page is a litteral, whereas its \emph{subject} is another resource. Adding more attributes\slash relationships in a query can be done using the ``and operator", as shown in Listing \ref{get_urls}.

In the following sections we show how the most common operations are performed in iSquirrel. 

\section{Getting page information from Dbpedia}
As shown in Listing \ref{pageInformation}, a list of properties describing a page can be retrieved when calling \emph{getPageInformation()}. The function first checks if the given page redirects somewhere else. In that case it changes the page with the redirection, and then collects all the \emph{properties} that describe the page. Line 41 checks whether a property is blacklisted. In that case it is ignored and not returned in the result.
\\
\lstset{language=Java}
\lstset{backgroundcolor=\color{white}}
\lstset{tabsize=2, showstringspaces=false} 
\lstset{keywordstyle=\color{red}\bfseries}
\begin{lstlisting}[frame=tb, caption=Getting page information, label=pageInformation]
 
public List<Property> getPageInformation(String wikipediaURL) {
	String dbPediaPage = URL.convertWIKIpediaToDBpedia(wikipediaURL);
	String query = "SELECT ?redirect WHERE { <" + dbPediaPage
			+ "> <http://dbpedia.org/property/redirect> ?redirect }";
	QueryExecution qexec = null;
	ResultSet results = null;
	try {
		qexec = QueryExecutionFactory.sparqlService(this.endpoint, query);
		results = qexec.execSelect();

		if (results != null && results.hasNext()) {
			QuerySolution soln = results.nextSolution();
			RDFNode redirect = soln.get("redirect");
			if (redirect != null) {
				dbPediaPage = redirect.toString();
			}
		}
	} catch (Exception ex) {
		System.err.println("EXCEPTION: " + ex.getMessage());
	} finally {
		if (qexec != null)
			qexec.close();
	}

	query = "SELECT ?attribute ?value WHERE { <" + dbPediaPage
			+ "> ?attribute ?value }";
	List<Property> list = new LinkedList<Property>();

	qexec = null;
	results = null;
	try {
		qexec = QueryExecutionFactory.sparqlService(this.endpoint, query);
		results = qexec.execSelect();

		while (results.hasNext()) {
			QuerySolution soln = results.nextSolution();
			if (soln.get("value").isResource()) {
				RDFNode attribute = soln.get("attribute");
				Resource value = soln.getResource("value");
				if (isAllowed(attribute.toString()) 
					&& isValueAllowed(value.toString())) {
					list.add(new Property(attribute.toString(), value
							.toString()));
				}
			}
		}
	} catch (Exception ex) {
		System.err.println("EXCEPTION: in getPageInformation " + ex.getMessage());

	} finally {
		if (qexec != null)
			qexec.close();
	}
	return list;
}

\end{lstlisting}

\section{Getting one level more specific \emph{subjects}}
As shown in Listing \ref{specific_subjects}, the ``http://www.w3.org/2004/02/skos/core\#broader" attribute is used to receive one level more specific subjects. This piece of code is used from the Static Recommender, when generating recommendations for a particular interest of a user.

\lstset{language=Java}
\lstset{backgroundcolor=\color{white}}
\lstset{tabsize=2,showstringspaces=false} 
\lstset{keywordstyle=\color{red}\bfseries}
\begin{lstlisting}[frame=tb, caption=Getting more specific subjects, label=specific_subjects]
public Set<String> getMoreSpecificSubjects(String subject) {
	Set<String> specific = new HashSet<String>();

		String query = "SELECT ?broader WHERE { ?broader"
				+ " <http://www.w3.org/2004/02/skos/core#broader> <"
				+ subject + "> }";

		QueryExecution qexec = null;
		ResultSet results = null;
		try {
			qexec = QueryExecutionFactory.sparqlService(this.endpoint,
					query);
			results = qexec.execSelect();

			while (results.hasNext()) {
				QuerySolution solution = results.nextSolution();
				Resource x = solution.getResource("broader");
				String dbPediaURL = x.toString();
				specific.add(dbPediaURL);
			}
		} catch (Exception ex) {
			System.err.println("EXCEPTION: in getMoreSpecificSubjects "
				 + ex.getMessage());
		} finally {
			if (qexec != null)
				qexec.close();
		}
	return specific;
}

\end{lstlisting}

\section{Getting urls from a list of \emph{properties}}
Listing \ref{get_urls} shows how to retrieve a set of Wikipedia articles that fulfil a set of properties. Line 14 uses the ``and operator" to specify that all properties should be matched, and line 29 converts the DBpedia page to a Wikipedia article.

\lstset{language=Java}
\lstset{backgroundcolor=\color{white}}
\lstset{tabsize=2, showstringspaces=false} 
\lstset{keywordstyle=\color{red}\bfseries}
\begin{lstlisting}[frame=tb, caption=Getting urls from a list of properties, label=get_urls]

public Set<String> getUrls(List<Property> properties) {
	if (properties == null || properties.size() == 0)
		return new HashSet<String>(); // return an empty list

	Set<String> result = new HashSet<String>();
	String queryString = "SELECT  DISTINCT ?x WHERE {";

	for (Iterator<Property> it = properties.iterator(); it.hasNext();) {
		Property property = it.next();
		queryString += " ?x <" + property.getAttribute() + "> <"
				+ property.getValue() + ">";
		if (it.hasNext())
			queryString += " .";
	}
	queryString += " }";

	QueryExecution qexec = null;
	ResultSet results = null;
	try {
		qexec = QueryExecutionFactory.sparqlService(this.endpoint,
				queryString);
		results = qexec.execSelect();

		for (; results.hasNext();) {
			QuerySolution solution = results.nextSolution();
			Resource x = solution.getResource("x");
			String dbPediaURL = x.toString();
			result.add(URL.convertDBpediaToWIKIpedia(dbPediaURL));
		}
	} catch (Exception ex) {
		System.err.println("EXCEPTION in getUrls: " + ex.getMessage());
	} finally {
		if (qexec != null)
			qexec.close();
	}
	return result;
}


\end{lstlisting}

